package org.hepeng.couchdb.demo;

import org.ektorp.CouchDbConnector;
import org.ektorp.CouchDbInstance;
import org.ektorp.ViewQuery;
import org.ektorp.ViewResult;
import org.ektorp.http.HttpClient;
import org.ektorp.http.StdHttpClient;
import org.ektorp.impl.StdCouchDbConnector;
import org.ektorp.impl.StdCouchDbInstance;
import org.junit.Before;
import org.junit.Test;

/**
 * author he peng
 * date 2022/4/26 0:50
 */
public class EktorpTest {

    CouchDbConnector couchDbConnector;

    @Before
    public void init() throws Exception {
        HttpClient httpClient = new StdHttpClient.Builder()
                .url("http://192.168.1.66:5984")
                .username("admin")
                .password("adminpw")
                .build();

        CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
        couchDbConnector = new StdCouchDbConnector("mychannel_campus-management-system-chaincode", dbInstance);
    }

    @Test
    public void viewTest1() {

        ViewQuery vq = new ViewQuery()
                .designDocId("_design/view1")
                .viewName("new-view")
                .reduce(false)
                .includeDocs(false);

        ViewResult viewResult = couchDbConnector.queryView(vq);
        System.out.println(viewResult);

    }

    @Test
    public void viewTest2() {
        EktorpViewRepository ektorpViewRepository = new EktorpViewRepository(Long.class, couchDbConnector);
        ektorpViewRepository.initStandardDesignDocument();
        Long count = ektorpViewRepository.count();
        System.out.println(count);
    }
}
